{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.先导入需要的模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们api_secret、api_key\n",
    "api_secret = \"72ZSxF3jN4mKki4f6kcTCDIjlEvcwi4S\"\n",
    "api_key = 'n5iuIaeoc-HEqvbWSEMxc73zfy092FdR'\n",
    "\n",
    "# 3.目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://www.sonystyle.com.cn/content/dam/sonystyle/products/ilc/e-body/ilce-7c/feature/ilce_7c_01_200915.jpg'\n",
    "\n",
    "# 4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5.准备后面的数据\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    # 是否检测并返回根据人脸特征判断出的年龄、性别、情绪等属性。\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "# 6.requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603520559,8439f94a-bcd4-4b56-af95-d9c44667f54c\",\"time_used\":1660,\"faces\":[{\"face_token\":\"1000f12b5009a1789cea0d64a9edaa61\",\"face_rectangle\":{\"top\":103,\"left\":515,\"width\":156,\"height\":156},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":21},\"smile\":{\"value\":0.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.047,\"disgust\":0.012,\"fear\":0.012,\"happiness\":0.013,\"neutral\":96.918,\"sadness\":0.099,\"surprise\":2.898}}}],\"image_id\":\"JxeiUPRe/peRD85pyPhE0w==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603520559,8439f94a-bcd4-4b56-af95-d9c44667f54c',\n",
       " 'time_used': 1660,\n",
       " 'faces': [{'face_token': '1000f12b5009a1789cea0d64a9edaa61',\n",
       "   'face_rectangle': {'top': 103, 'left': 515, 'width': 156, 'height': 156},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.047,\n",
       "     'disgust': 0.012,\n",
       "     'fear': 0.012,\n",
       "     'happiness': 0.013,\n",
       "     'neutral': 96.918,\n",
       "     'sadness': 0.099,\n",
       "     'surprise': 2.898}}}],\n",
       " 'image_id': 'JxeiUPRe/peRD85pyPhE0w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
